package com.stylefeng.guns.modular.operation_log.service.impl;

import com.stylefeng.guns.modular.apple.utils.ExcelUtil;
import com.stylefeng.guns.modular.apple.utils.TimeUtil;
import com.stylefeng.guns.modular.apple.vo.Paging;
import com.stylefeng.guns.modular.apple_id_manage.model.AppleIds;
import com.stylefeng.guns.modular.operation_log.bo.OperationLogBO;
import com.stylefeng.guns.modular.operation_log.model.MOperationLog;
import com.stylefeng.guns.modular.operation_log.dao.MOperationLogMapper;
import com.stylefeng.guns.modular.operation_log.service.IMOperationLogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-10-01
 */
@Service
public class MOperationLogServiceImpl extends ServiceImpl<MOperationLogMapper, MOperationLog> implements IMOperationLogService {

    @Override
    public Object list(Integer page, Integer size) {
        int start = (page>0?page-1:page) * size;
        Paging<OperationLogBO> paging = new Paging<>();
        Integer total = this.baseMapper.selectCount(null);

        paging.setTotal(total.longValue());

        List<OperationLogBO> items = this.baseMapper.findListOfPage(start,size);

        paging.setItems(items);

        return paging;
    }

    @Override
    public void export(HttpServletResponse response) {
        List<OperationLogBO> items = this.baseMapper.findAllLog();

        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个Excel表单，参数为sheet的名字
        HSSFSheet sheet = workbook.createSheet("设备添加数据");
        List<String> colList = new ArrayList<>();
        colList.add("商户名称");
        colList.add("操作人");
        colList.add("设备数量");
        colList.add("操作类型");
        colList.add("操作时间");
        //创建表头
        ExcelUtil.setTableHeader(workbook,sheet,colList);

        //3、插入内容数据
        int rowNum = 1;
        for (OperationLogBO logBO: items){
            HSSFRow row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(logBO.getMerchantName());
            row.createCell(1).setCellValue(logBO.getUserName());
            row.createCell(2).setCellValue(logBO.getOperationContent());
            row.createCell(3).setCellValue(logBO.getType() == 1?"添加设备":"");
            row.createCell(4).setCellValue(TimeUtil.formatDate(logBO.getCreateTime(),"yyyy-MM-dd"));
            rowNum++;
        }
        String fileName = "DeviceLog-"+System.currentTimeMillis()+".xls";
        //写入文件并将文件已二进制流的方式返回为调用者
        ExcelUtil.writeWorkBook(response,workbook,fileName);
        //4、写入文件并将文件已二进制流的方式返回为调用者
    }
}
